package com.wll.ec.fast.main.personal.profile;

import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;

import com.wll.ec.core.delegates.EcBaseDelegate;
import com.wll.ec.core.net.RestClient;
import com.wll.ec.core.net.callback.ISuccess;
import com.wll.ec.core.util.log.EcLogger;
import com.wll.ec.fast.R;
import com.wll.ec.fast.R2;
import com.wll.ec.fast.main.personal.PersonalItemType;
import com.wll.ec.fast.main.personal.list.ListAdapter;
import com.wll.ec.fast.main.personal.list.ListBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * created by detachment on 2020/3/26
 */
public class UserProfileDelegate extends EcBaseDelegate {

    ListAdapter adapter = null;

    @BindView(R2.id.rv_user_profile)
    RecyclerView mRecyclerView = null;

    @Override
    public Object setLayout() {
        return R.layout.delegate_user_profile;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        final ListBean image = new ListBean.Builder()
                .setItemType(PersonalItemType.ITEM_AVATAR)
                .setId(UserProfileFields.ID)
                .setImageUrl("https://avatar.csdnimg.cn/E/C/7/1_aha_jasper_1584566362.jpg")
                .build();
        final ListBean name = new ListBean.Builder()
                .setItemType(PersonalItemType.ITEM_NORMAL)
                .setId(UserProfileFields.NAME)
                .setText("姓名")
                .setDelegate(new NameDelegate())
                .setValue("未设置姓名")
                .build();
        final ListBean gender = new ListBean.Builder()
                .setItemType(PersonalItemType.ITEM_NORMAL)
                .setId(UserProfileFields.GENDER)
                .setText("性别")
                .setValue("未设置性别")
                .build();
        final ListBean birth = new ListBean.Builder()
                .setItemType(PersonalItemType.ITEM_NORMAL)
                .setId(UserProfileFields.BIRTHDAY)
                .setText("生日")
                .setValue("未设置生日")
                .build();

        final List<ListBean> data = new ArrayList<>();
        data.add(image);
        data.add(name);
        data.add(gender);
        data.add(birth);

        //设置RecyclerView
        final LinearLayoutManager manager = new LinearLayoutManager(getContext());
        mRecyclerView.setLayoutManager(manager);

        adapter = new ListAdapter(data);
        mRecyclerView.setAdapter(adapter);
        mRecyclerView.addOnItemTouchListener(new UserProfileClickListener(this));

        RestClient.builder()
                .url("user_profile.php")
                .loader(getContext())
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        EcLogger.d("UserProfileDelegate response: ", response);
                        adapter.updateUserProfile(response);
                    }
                })
                .build()
                .get();
    }
}
